home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex7-1.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  6KB  |  203 lines

  1. // ex7-1.c -- Geometry class hierarchy using NIH Class Library
  2.  
  3. #include "Point.h"
  4. #include "Stack.h"
  5. #include "OrderedCltn.h"
  6.  
  7. class TransformStack {
  8.     Stack s;
  9. public:
  10.     TransformStack()        { s.push(*new Point(0,0)); }
  11.     Point* current() const  { return (Point*)s.top(); }
  12.     void push(Point& p)     { s.push(*new Point(*current() + p)); }
  13.     void pop()              { delete (Point*)s.pop(); }
  14. };
  15.  
  16. TransformStack transform;   // shape translation stack
  17.  
  18. class Shape: public Object {
  19.     DECLARE_MEMBERS(Shape);
  20.     Point org;          // origin
  21. protected:
  22.     Shape(const Point& p) : org(p)  {}
  23. public:
  24.     Point origin() const                { return org; }
  25.     virtual void move(const Point& d)   { org += d; }
  26.     virtual void draw() const = 0;
  27. private:                // shouldNotImplement()
  28.     virtual int compare(const Object&) const;
  29.     virtual unsigned hash() const;
  30.     virtual bool isEqual(const Object&) const;
  31.     virtual void printOn(ostream& strm =cout) const;
  32. protected:              // shouldNotImplement()
  33.     virtual void deepenShallowCopy();
  34. };
  35.  
  36. #define THIS Shape
  37. #define BASE Object
  38. #define BASE_CLASSES Object::desc()
  39. #define MEMBER_CLASSES Point::desc()
  40. #define VIRTUAL_BASE_CLASSES
  41.  
  42. DEFINE_ABSTRACT_CLASS(Shape,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex7-1.c,v 3.0 90/05/15 22:45:47 kgorlen Rel $",NULL,NULL);
  43.  
  44. #undef  THIS
  45. #undef  BASE
  46. #undef  BASE_CLASSES
  47. #undef  MEMBER_CLASSES
  48. #undef  VIRTUAL_BASE_CLASSES
  49.  
  50. class Line: public Shape {
  51.     DECLARE_MEMBERS(Line);
  52.     Point p;            // end point
  53. public:
  54.     Line(const Point& a, const Point& b) : Shape(a), p(b) {}
  55.     virtual void move(const Point&);
  56.     virtual void draw() const;
  57. };
  58.  
  59. #define THIS Line
  60. #define BASE Shape
  61. #define BASE_CLASSES Shape::desc()
  62. #define MEMBER_CLASSES Point::desc()
  63. #define VIRTUAL_BASE_CLASSES
  64.  
  65. DEFINE_CLASS(Line,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex7-1.c,v 3.0 90/05/15 22:45:47 kgorlen Rel $",NULL,NULL);
  66.  
  67. void Line::move(const Point& d)
  68. {
  69.     Shape::move(d);
  70.     p += d;
  71. }
  72.  
  73. void Line::draw() const
  74. {
  75.     cout << "Line from " << *transform.current() + origin()
  76.          << " to " << *transform.current() + p << endl;
  77. }
  78.  
  79. #undef  THIS
  80. #undef  BASE
  81. #undef  BASE_CLASSES
  82. #undef  MEMBER_CLASSES
  83. #undef  VIRTUAL_BASE_CLASSES
  84.  
  85. class Circle: public Shape {
  86.     DECLARE_MEMBERS(Circle);
  87.     int rad;            // radius of circle
  88. public:
  89.     Circle(const Point& c, int r) : Shape(c) { rad = r; }
  90.     virtual void draw() const;
  91. };
  92.  
  93. #define THIS Circle
  94. #define BASE Shape
  95. #define BASE_CLASSES Shape::desc()
  96. #define MEMBER_CLASSES
  97. #define VIRTUAL_BASE_CLASSES
  98.  
  99. DEFINE_CLASS(Circle,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex7-1.c,v 3.0 90/05/15 22:45:47 kgorlen Rel $",NULL,NULL);
  100.  
  101. void Circle::draw() const
  102. {
  103.     cout << "Circle with center "
  104.          << *transform.current() + origin()
  105.          << " and radius " << rad << endl;
  106. }
  107.  
  108. #undef  THIS
  109. #undef  BASE
  110. #undef  BASE_CLASSES
  111. #undef  MEMBER_CLASSES
  112. #undef  VIRTUAL_BASE_CLASSES
  113.  
  114. class Picture: public Shape {
  115.     DECLARE_MEMBERS(Picture);
  116.     OrderedCltn s;      // collection of pointers to shapes
  117. public:
  118.     Picture() : Shape(Point(0,0)) {}    // constructor
  119.     Picture(Point& org) : Shape(org) {} // constructor
  120.     void add(Shape&);                   // add Shape to Picture
  121.     virtual void draw() const;          // draw picture;
  122. };
  123.  
  124. #define THIS Picture
  125. #define BASE Shape
  126. #define BASE_CLASSES Shape::desc()
  127. #define MEMBER_CLASSES OrderedCltn::desc()
  128. #define VIRTUAL_BASE_CLASSES
  129.  
  130. DEFINE_CLASS(Picture,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex7-1.c,v 3.0 90/05/15 22:45:47 kgorlen Rel $",NULL,NULL);
  131.  
  132. void Picture::add(Shape& t)
  133. {
  134.     s.add(t);           // calls OrderedCltn::add()
  135. }
  136.  
  137. void Picture::draw() const      // draw a Picture
  138. {
  139.     transform.push(origin());
  140.     for (int i=0; i<s.size(); i++)  // s.size() is # of
  141.                                     // objects in s
  142.         ((Shape*)s[i])->draw();     // cast address of ith object
  143.                                     // to Shape* and call draw()
  144.     transform.pop();
  145. }
  146.  
  147. #undef  THIS
  148. #undef  BASE
  149. #undef  BASE_CLASSES
  150. #undef  MEMBER_CLASSES
  151. #undef  VIRTUAL_BASE_CLASSES
  152.  
  153. main()
  154. {
  155.     Line l(Point(1,2),Point(3,4));  // create a Line
  156.     Circle c(Point(5,6),1);         // create a Circle
  157.     l.draw();                       // draw the line
  158.     c.draw();                       // draw the circle
  159.     Picture p;                      // create an empty Picture
  160.     l.move(Point(1,0));             // move the line
  161.     p.add(l);                       // add the line to the picture
  162.     c.move(Point(1,0));             // move the circle
  163.     p.add(c);                       // add it to the picture
  164.     p.draw();                       // draw the picture
  165.     p.move(Point(10,10));           // translate it by (10,10)
  166.     p.draw();                       // draw it again
  167.  
  168.     Picture bigPic;
  169.     Picture littlePic1, littlePic2(*new Point(10,10));
  170.     littlePic1.add(*new Line(Point(1,1),Point(2,2)));
  171.     littlePic1.add(*new Circle(Point(3,3),1));
  172.     littlePic2.add(*new Line(Point(4,4),Point(5,5)));
  173.     littlePic2.add(*new Circle(Point(3,3),2));
  174.     littlePic2.move(Point(1,1));
  175.     bigPic.add(littlePic1);
  176.     bigPic.add(littlePic2);
  177.     bigPic.draw();
  178. }
  179.  
  180. // Dummy definitions for functions required by NIH Class Library
  181.  
  182. int Shape::compare(const Object&) const
  183.        { shouldNotImplement("compare"); return 0; }
  184. void Shape::deepenShallowCopy()
  185.        { shouldNotImplement("deepenShallowCopy"); }
  186. unsigned Shape::hash() const
  187.        { shouldNotImplement("hash"); return 0; }
  188. bool Shape::isEqual(const Object&) const
  189.        { shouldNotImplement("isEqual"); return 0; }
  190. void Shape::printOn(ostream&) const
  191.        { shouldNotImplement("printOn"); }
  192. Shape::Shape(OIOifd& fd) : Object(fd) {}
  193. Shape::Shape(OIOin& strm) : Object(strm) {}
  194.  
  195. Line::Line(OIOifd& fd) : Shape(fd) {}
  196. Line::Line(OIOin& strm) : Shape(strm) {}
  197.  
  198. Circle::Circle(OIOifd& fd) : Shape(fd) {}
  199. Circle::Circle(OIOin& strm) : Shape(strm) {}
  200.  
  201. Picture::Picture(OIOifd& fd) : Shape(fd) {}
  202. Picture::Picture(OIOin& strm) : Shape(strm) {}
  203.